﻿using AnyLink.Domain;
using AnyLink.Domain.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;

namespace AnyLink.WebApi.Controllers
{
    public class BaseController : ControllerBase
    {
        /// <summary>
        /// 返回成功结果集
        /// </summary>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        protected virtual IActionResult Ok<T>(string message, T data = default)
        {
            return Ok(RestResult(StatusCodes.Status200OK, true, message, data));
        }

        /// <summary>
        /// 返回失败结果集
        /// </summary>
        /// <param name="message"></param>
        /// <param name="code"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        protected virtual IActionResult Error<T>(string message, int code = StatusCodes.Status400BadRequest, T data = default)
        {
            return Ok(RestResult(code, false, message, data));
        }

        /// <summary>
        /// 返回成功结果集
        /// </summary>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        protected virtual IActionResult Ok(string message)
        {
            return Ok(RestResult(StatusCodes.Status200OK, true, message));
        }

        /// <summary>
        /// 返回失败结果集
        /// </summary>
        /// <param name="message"></param>
        /// <param name="code"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        protected virtual IActionResult Error(string message, int code = StatusCodes.Status400BadRequest)
        {
            return Ok(RestResult(code, false, message));
        }

        /// <summary>
        /// 返回 RESTful 风格结果集
        /// </summary>
        /// <param name="statusCode"></param>
        /// <param name="succeeded"></param>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        protected virtual ResultJsonView<T> RestResult<T>(int statusCode, bool succeeded = default, string msg = default, T data = default)
        {
            return new ResultJsonView<T>
            {
                Code = statusCode,
                Message = msg, //msg is null or string ? (msg + "") : JSON.Serialize(msg),
                Data = data,
                Type = succeeded ? "success" : "error"
            };
        }

        /// <summary>
        /// 返回 RESTful 风格结果集
        /// </summary>
        /// <param name="statusCode"></param>
        /// <param name="succeeded"></param>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        protected virtual ResultJsonView RestResult(int statusCode, bool succeeded = default, string msg = default)
        {
            return new ResultJsonView
            {
                Code = statusCode,
                Message = msg, //msg is null or string ? (msg + "") : JSON.Serialize(msg),
                Type = succeeded ? "success" : "error"
            };
        }


        public static string TIME_FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";

        /// <summary>
        /// json输出带时间格式的
        /// </summary>
        /// <param name="apiResult"></param>
        /// <returns></returns>
        protected IActionResult ToResponse(ApiResult apiResult)
        {
            string jsonStr = ToJsonApiResult(apiResult, TIME_FORMAT_FULL);

            return Content(jsonStr, "application/json");
        }

        /// <summary>
        /// json输出
        /// </summary>
        /// <param name="apiResult"></param>
        /// <param name="timeFormatStr"></param>
        /// <returns></returns>
        private static string ToJsonApiResult(ApiResult apiResult, string timeFormatStr)
        {
            if (string.IsNullOrEmpty(timeFormatStr))
            {
                timeFormatStr = TIME_FORMAT_FULL;
            }
            var serializerSettings = new JsonSerializerSettings
            {
                // 设置为驼峰命名
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
                DateFormatString = timeFormatStr
            };

            return JsonConvert.SerializeObject(apiResult, Formatting.Indented, serializerSettings);
        }
    }
}
